package com.yuanfeng.base.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.dto.BaseSmsConfigDTO;
import com.yuanfeng.base.entity.BaseSmsInfoEntity;
import com.yuanfeng.base.mapper.BaseSmsInfoMapper;
import com.yuanfeng.base.service.BaseSmsInfoService;
import com.yuanfeng.commoms.config.YFConfig;
import com.yuanfeng.commoms.constant.CommonType;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class BaseSmsInfoServiceImpl extends ServiceImpl<BaseSmsInfoMapper, BaseSmsInfoEntity>
    implements BaseSmsInfoService{

    @Override
    public ResponseResult selectSMSInfo2() {
        List<Map<String,Object>> list = new ArrayList<>();
        //接收返回信息
        BaseSmsInfoEntity entity= this.baseMapper.selectOne(new LambdaQueryWrapper<BaseSmsInfoEntity>());
        if(null != entity ){
            Integer flag = 1;
            RedisUtil.set("SMS_CONFIG", JSON.toJSON(entity).toString());
            entity.setFlag(flag);
            if(null != entity.getSmsGeneral()){
                Map<String,Object> template =new HashMap<>();
                template.put("key","通用模板");
                template.put("name","smsGeneral");
                template.put("value",entity.getSmsGeneral());
                template.put("desc","您的验证码是{1}，5分钟内有效。为了保护您的账号安全，验证短信请勿转发他人。如非本人操作，请忽略本短信。");
                list.add(template);
            }
            if(null != entity.getSmsInventory()){
                Map<String,Object> template =new HashMap<>();
                template.put("key","商品库存不足模板");
                template.put("name","smsInventory");
                template.put("value",entity.getSmsInventory());
                template.put("desc","您的商品{1}库存即将耗尽，请尽快补充调整。");
                list.add(template);
            }
            if(null != entity.getSmsShipments()){
                Map<String,Object> template =new HashMap<>();
                template.put("key","商家发货成功模板");
                template.put("name","smsShipments");
                template.put("value",entity.getSmsShipments());
                template.put("desc","您好，您在{1}店铺购买的商品商家已经发货，物流单号为{2}，物流公司是{3}，请确认收货。");
                list.add(template);
            }
            if(null != entity.getSmsPushTheDelivery()){
                Map<String,Object> template =new HashMap<>();
                template.put("key","提醒商家发货模板");
                template.put("name","smsPushTheDelivery");
                template.put("value",entity.getSmsPushTheDelivery());
                template.put("desc","发货提醒：{1}在您店铺购买的商品已超过{2}小时未发货，订单号为{3}，请尽快安排发货，如超过48小时未发货，平台将对您的商品进行下架处理");
                list.add(template);
            }
            if(null != entity.getSmsGoodsObligation()){
                Map<String,Object> template =new HashMap<>();
                template.put("key","提醒用户付款模板");
                template.put("name","smsGoodsObligation");
                template.put("value",entity.getSmsGoodsObligation());
                template.put("desc","商品待付款提醒：您在{1}店铺购买的{2}商品未支付，请尽快支付。");
                list.add(template);
            }
            if(null != entity.getSmsPlusMember()){
                Map<String,Object> template =new HashMap<>();
                template.put("key","PLUS权益到账提醒");
                template.put("name","smsPlusMember");
                template.put("value",entity.getSmsPlusMember());
                template.put("desc","远丰商城：尊敬的用户，您好！您已获得PLUS{1}权益，将于24小时内到达账户中心，您可登录网站查看www.yuanfeng.com");
                list.add(template);
            }
            entity.setTemplateList(list);
        }
        //oss配置库存在配置信息并返回给前端做展示
        return ResponseResult.success( "SMS配置信息存在并返回成功",entity);
    }


    @Override
    public ResponseResult updateSmsConfig(BaseSmsConfigDTO baseSmsConfigReq) {
        if (null != baseSmsConfigReq && null != baseSmsConfigReq.getToken()) {
            //校验token
            String token = baseSmsConfigReq.getToken();
            UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(token)==null? ParseTokenUtils.getPlatformUser(token):ParseTokenUtils.getUserInfoByToken(token);
            if (null != userInfo) {
                BaseSmsInfoEntity smsInfoEntity = this.baseMapper.selectOne(new QueryWrapper<>());
                if (smsInfoEntity == null) {
                    smsInfoEntity = new BaseSmsInfoEntity();
                    BeanUtils.copyProperties(baseSmsConfigReq, smsInfoEntity);
                    //新增sms 配置
                    smsInfoEntity.setSmsStatus(CommonType.smsEnableDisableStatus.STARTUSING.getCode().toString());
                    smsInfoEntity.setSmsIsactive(CommonType.pseudoDeleteIdentification.NORMAL.getCode().toString());
                    //oss配置库没有配置信息就初始化数据
                    this.baseMapper.insert(smsInfoEntity);
                    RedisUtil.set(YFConfig.SMS_CONFIG, JSON.toJSONString(smsInfoEntity));
                } else {
                    //修改sms 配置
                    BeanUtils.copyProperties(baseSmsConfigReq, smsInfoEntity);
                    this.baseMapper.updateById(smsInfoEntity);
                    //smsInfoMapper.updateSmsConfig(baseSmsConfigReq);
                    RedisUtil.remove(YFConfig.SMS_CONFIG);
                    RedisUtil.set(YFConfig.SMS_CONFIG, JSON.toJSONString(smsInfoEntity));
                }
                return ResponseResult.success();
            }
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
        }
        return ResponseResult.fail(BizCodeEnume.VALID_EXCEPTION);
    }

    @Override
    public ResponseResult updateSMS(Map<String, String> param) {
        this.baseMapper.updateSMS(param);
        RedisUtil.set(YFConfig.SMS_CONFIG, JSON.toJSON(this.baseMapper.selectOne(new QueryWrapper<>())).toString());
        return ResponseResult.success();
    }
}




